package com.aim.Activity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import com.aim.Service.UploadPicService;
import com.aim.api.AIMCaseConstants;
import com.aim.util.DBAdapter;
import com.aim.util.KeyAndValuePair;
import com.aim.util.WebServiceUtil;

public class ImpoundTab extends TabActivity implements OnClickListener, OnTabChangeListener{
	
	Context mC;
	final public static int LIMIT = 10;
	static int impoundState; // 0 = new, 1 = edit, 2 = local
	static int  mark = 0, picLimit = LIMIT, picNo = 0;
	static String caseNo,no,vin, make, model, year, note, add, city, state, zip,tow,title,color,type,plate,mileage;
	static List<Map<String, Object>> list; 
	static boolean bookmark;

	Button buttonCancel;
	Button buttonSave;
	DBAdapter mDBAdapter;
	TabHost tabHost;
	String mode;
	private ProgressDialog progressDialog;	
    private Handler handler = new Handler(){  
    	  
        @Override  
        public void handleMessage(Message msg) {  
              
        	if(msg.what ==1)
            {
	            	progressDialog.dismiss(); 
	            	
	        		if (!caseNo.equals("0")) {
	        			Intent i = new Intent(ImpoundTab.this, UploadPicService.class);
	                	i.putExtra("caseno", caseNo);
//	                	i.putExtra("picno", picNo);
	                	startService(i);
	        			Toast.makeText(mC, "Create Case Success", Toast.LENGTH_LONG).show();
	        			
	        	} else {
	        		mDBAdapter.open();
	        		mDBAdapter.insertCase(vin, make, model,color, type,  year, plate, mileage, add, city, state, zip, tow, mark,title, note);
			    	changePicNameToLocal();
			    	mDBAdapter.close();
	        		Toast.makeText(mC, "Create Case Fail", Toast.LENGTH_LONG).show();
	        	
	        	}
	        		ImpoundTab.this.finish();
        			
            }else if(msg.what ==2)
            {
	            	progressDialog.dismiss();
	            	
	        		if (!caseNo.equals("0")) {
	        			Intent i = new Intent(ImpoundTab.this, UploadPicService.class);
	                	i.putExtra("caseno", caseNo);
//	                	i.putExtra("picno", picNo);
	                	startService(i);
	        			Toast.makeText(mC, "Edit Case Success, please refresh your impoundlist", Toast.LENGTH_LONG).show();
	        			
	        	} else {
	        		Toast.makeText(mC, "Edit Case Fail", Toast.LENGTH_LONG).show();
	
	        	}
	        		ImpoundTab.this.finish();
	            }
	          
        }
        };
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        mC = this;
        setContentView(R.layout.tab);


        
        
        mDBAdapter = new DBAdapter(this);
//		mDBAdapter.open();
        Bundle extras = getIntent().getExtras();        
        if (extras != null) {
        	mode = extras.getString("MODE");     
        }

        	if(mode.equals("new"))
        {
        	impoundState = 0;
           
            vin = extras.getString("VIN");
            }      

        else if (mode.equals("edit"))
        {
        impoundState = 1;
        no = extras.getString("NO");	

        vin = extras.getString("VIN");	
        make = extras.getString("MAKE");
        model = extras.getString("MODEL");
        year = extras.getString("YEAR");
        color = extras.getString("COLOR");
        plate = extras.getString("PLATE");
        mileage = extras.getString("MILEAGE");
        type = extras.getString("TYPE");
        tow = extras.getString("TOW");
        add = extras.getString("ADD");
        city = extras.getString("CITY");
        state = extras.getString("STATE");
        zip = extras.getString("ZIP");
        
        int count =0, noteNo;
        
        String a = extras.getString("IMAGECOUNT");
        if(a!=null)
        count = Integer.parseInt(a);
        picLimit = LIMIT - count;
        
        
        
        noteNo = extras.getInt("NOTENO");
        
        Map<String, Object> map; 

        

        list=new ArrayList<Map<String,Object>>(); 
        
        for(int j = 0; j<noteNo;j++){
        	  map = new HashMap<String, Object>();

        	  String seqNo = extras.getString("SEQNO"+j);
        	  map.put("seqNo", seqNo);	
				
        	  String noteTitle = extras.getString("NOTETITLE"+j);
        	  map.put("noteTitle", noteTitle);	

        	  String notes = extras.getString("NOTES"+j);
        	  map.put("notes", notes);	
        	  
        	  String bookmark = extras.getString("BOOKMARK"+j);
        	  map.put("bookmark", bookmark);	
        	  
              list.add(map); 
        	
        }
       
        
        }
        else if (mode.equals("local"))
        {
        impoundState = 2;
        no = extras.getString("NO");
        vin = extras.getString("VIN");	
        make = extras.getString("MAKE");
        model = extras.getString("MODEL");
        year = extras.getString("YEAR");
        color = extras.getString("COLOR");
        plate = extras.getString("PLATE");
        mileage = extras.getString("MILEAGE");
        type = extras.getString("TYPE");
        tow = extras.getString("TOW");
        add = extras.getString("ADD");
        city = extras.getString("CITY");
        state = extras.getString("STATE");
        zip = extras.getString("ZIP");
        title = extras.getString("TITLE");
        note = extras.getString("NOTE");
        mark = Integer.parseInt(extras.getString("MARK"));
        
        int count =0;
        
        String a = extras.getString("IMAGECOUNT");
        if(a!=null)
        count = Integer.parseInt(a);
        picLimit = LIMIT - count;

        
        
        }
        	
        
        /* TabHost will have Tabs */
        tabHost = (TabHost)findViewById(android.R.id.tabhost);
        
        /* TabSpec used to create a new tab. 
         * By using TabSpec only we can able to setContent to the tab.
         * By using TabSpec setIndicator() we can set name to tab. */
        
        /* tid1 is firstTabSpec Id. Its used to access outside. */
        TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
        TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
        TabSpec thirdTabSpec = tabHost.newTabSpec("tid3");
        TabSpec forthTabSpec = tabHost.newTabSpec("tid4");
        
        Resources res = getResources();
		firstTabSpec.setIndicator("Details",res.getDrawable(R.drawable.tab_detail)).setContent(new Intent(this,Details.class));

        
		secondTabSpec.setIndicator("Photo",res.getDrawable(R.drawable.tab_photo)).setContent(new Intent(this,ShowPhotos.class));

       
        thirdTabSpec.setIndicator("Address",res.getDrawable(R.drawable.tab_address)).setContent(new Intent(this,Address.class));
        

        forthTabSpec.setIndicator("Note",res.getDrawable(R.drawable.tab_note)).setContent(new Intent(this,Notes.class));
        /* Add tabSpec to the TabHost to display. */
        
    	TabWidget tabWidget = tabHost.getTabWidget();
    	


        
        
        tabHost.addTab(firstTabSpec);
        tabHost.addTab(secondTabSpec);
        tabHost.addTab(thirdTabSpec);
        tabHost.addTab(forthTabSpec);
        

        tabHost.setOnTabChangedListener(this);
        
    	for (int i =0; i < tabWidget.getChildCount(); i++) {
//    		tabWidget.setBackgroundColor(Color.BLUE);
    		tabWidget.getChildAt(i).getLayoutParams().height = 120;
    		TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
    		tabWidget.getChildAt(i).setBackgroundColor(Color.GRAY);
//    		tv.setBackgroundColor(Color.GREEN);
    		tv.setTextColor(Color.WHITE);
    		tv.setTextSize(18);
//    		tv.setTextColor(R.color.Blue);
//    		tv.s
	    }
    	
    	
    	tabWidget.getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#f36c29"));
	
    	
        buttonCancel = (Button) findViewById(R.id.button1);
		buttonCancel.setOnClickListener(this);
		buttonSave = (Button) findViewById(R.id.button2);
		buttonSave.setOnClickListener(this);
		
//		changeTab();
    }
    

    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
//      setContentView(R.layout.myLayout);
      Log.e("Configuration ","Changed");

    } 
    
    @Override
	protected void onRestart(){
    	super.onRestart();
//    	mDBAdapter.open();
    }
    @Override
	protected void onStop(){
    	super.onStop();
//    	mDBAdapter.close();
    }

    @Override
	public void finish(){
    	no = null;
    	vin= null;
    	make=null;
    	model=null;
    	color=null;
    	type=null;
    	year=null;
    	plate=null;
    	mileage=null;
    	picLimit = LIMIT;
    	add=null;
    	city=null;
    	state=null;
    	zip=null;
    	tow=null;
    	title=null;
    	note=null;
    	buttonCancel = null;
    	buttonSave = null;
    	
    	tabHost = null;
    	mode = null;
    	progressDialog = null;
//    	mDBAdapter.close();
    	
    	list = null;
    	mC = null;
    	int i = 1;
    	while(i<=LIMIT)
    	{
    		String picname= AIMCaseConstants.SAVEDDIRECTORY+AIMCaseConstants.QUICKIMAGENAME+i+".jpg";
    	    File mF = new File(picname);
    	    mF.delete();
    	    i++;

    	}
    	ShowPhotos.picNo = 0;
    	System.gc();
    	super.finish();
    }
    @Override
	public void onDestroy(){
    	super.onDestroy();
    
    }



    
    public void onClick(View v) {
    	if(v.equals(buttonSave)){
    		
                    AlertDialog dlg = new AlertDialog.Builder(this)
                    .setTitle("Sure to Save?")
                    .setMessage("Make Sure All the Info and Pictures are correct.")
                    .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        	                     
                        	try {             		
                        		Details.setDetails();
							} catch (Exception e) {
								e.printStackTrace();
							}
							
							try {
								Address.setAddress();
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							
							try {
								Notes.setNotes();
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							
							try {
								ShowPhotos.setPhotos();
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							
							 //NETWORK DETECTION
					        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
					        NetworkInfo netInfo = cm.getActiveNetworkInfo();
//					        NetworkInfo netInfo = cm.getNetworkInfo(0);				        
					        //0 is mobile, 1 is WIFI

                        		if(impoundState ==0)
	                        	{
	                        		if (netInfo != null && netInfo.isConnected()) {
	    					     
	    					        	createCaseProgress();
					        		
						            	}
	                        		else 
	                        		{
							        	//insert and changepicname
	                        			mDBAdapter.open();
							        	mDBAdapter.insertCase(vin, make, model,color, type,  year, plate, mileage, add, city, state, zip, tow, mark,title, note);
								    	changePicNameToLocal();
								    	mDBAdapter.close();
								    	ImpoundTab.this.finish();
									
							        }
	                        	}else if (impoundState ==1)
                            	{ 
                            		
                            		editCaseProgress();
                            		
                            		
    			        		}else if (impoundState ==2)
						        {
    			        			mDBAdapter.open();
						        	String id = ImpoundTab.no.substring(5);
						        	int i = Integer.parseInt(id);
						        	mDBAdapter.updateCase(i,vin, make, model,color, type,  year, plate, mileage, add, city, state, zip, tow, mark,title, note);
						        	mDBAdapter.close();
						        	ImpoundTab.this.finish();
						        }
                        	
                        	
				            	ShowPhotos.picNo = 0;
					       
					        
					    	
					        
                            /* User clicked OK so do some stuff */
//                			ImpoundTab.this.finish();
                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        	dialog.dismiss();
                            /* User clicked cancel so do some stuff */
                        }
                    })
                    .create();
                    dlg.show();
                            	    		
    	}
    else if (v.equals(buttonCancel)) {
    	 AlertDialog dlg = new AlertDialog.Builder(this)
         .setTitle("Sure to Cancel?")
         .setMessage("After Canceling all the infomation will be lost")
         .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int whichButton) {
             	                     
//            	 uploadImage(1);
             	
            	ImpoundTab.this.finish();
            	 
             }
         })
         .setNegativeButton("No", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int whichButton) {
//             	dialog.dismiss();
                 /* User clicked cancel so do some stuff */

             }
         })
         .create();
         dlg.show();
         

	} 
}

    public void changePicNameToCase(){
    	
    	
	
		int i = 1;
    	while(i<=LIMIT){
		String picname= AIMCaseConstants.SAVEDDIRECTORY+AIMCaseConstants.QUICKIMAGENAME+i+".jpg";
		File a = new File(picname); 
	    if(a.exists())
	    {	picname = AIMCaseConstants.SAVEDDIRECTORY+caseNo+"img"+i+".jpg";

	    	File b = new File(picname);
	    	a.renameTo(b);
	    
	    
	    }
	    i++;
    	}
    	
    }
     
    
	
    public void changePicNameToLocal(){
    	mDBAdapter.open();
    	int localNo = mDBAdapter.getLocalCaseNo();
    	mDBAdapter.close();
		int i = 1;
    	while(i<=LIMIT){
		String picname= AIMCaseConstants.SAVEDDIRECTORY+AIMCaseConstants.QUICKIMAGENAME+i+".jpg";
	    File a = new File(picname); 
	    if(a.exists())
	    {
	    	picname = AIMCaseConstants.SAVEDDIRECTORY+AIMCaseConstants.LOCALIMAGENAME+localNo+"img"+i+".jpg";
	    	File b = new File(picname);
	    
	    	a.renameTo(b);
	    }
	    i++;
    	}
    	
    }
    
    
    public String createCase(){

		ArrayList parameterList = new ArrayList();
		
		KeyAndValuePair pictureCountPair = new KeyAndValuePair();
		pictureCountPair.setKeyName("howmanyPicture");
		pictureCountPair.setKeyValue("0");
		parameterList.add(0, pictureCountPair);

		KeyAndValuePair carMakePair = new KeyAndValuePair();
		carMakePair.setKeyName("carMake");
		carMakePair.setKeyValue(make);
		parameterList.add(1, carMakePair);

		KeyAndValuePair carModelPair = new KeyAndValuePair();
		carModelPair.setKeyName("carModel");
		carModelPair.setKeyValue(model);
		parameterList.add(2, carModelPair);

		KeyAndValuePair carColorPair = new KeyAndValuePair();
		carColorPair.setKeyName("carColor");
		carColorPair.setKeyValue(color);
		parameterList.add(3, carColorPair);

		KeyAndValuePair carTypePair = new KeyAndValuePair();
		carTypePair.setKeyName("carType");
		carTypePair.setKeyValue(type);
		parameterList.add(4, carTypePair);
		
		KeyAndValuePair carYearPair = new KeyAndValuePair();
		carYearPair.setKeyName("carYear");
		carYearPair.setKeyValue(year);
		parameterList.add(5, carYearPair);

		KeyAndValuePair vinPair = new KeyAndValuePair();
		vinPair.setKeyName("vin");
		vinPair.setKeyValue(vin);
		parameterList.add(6, vinPair);

		KeyAndValuePair addressPair = new KeyAndValuePair();
		addressPair.setKeyName("address");
		addressPair.setKeyValue(add);
		parameterList.add(7, addressPair);
		
		KeyAndValuePair cityPair = new KeyAndValuePair();
		cityPair.setKeyName("city");
		cityPair.setKeyValue(city);
		parameterList.add(8, cityPair);
		
		KeyAndValuePair statePair = new KeyAndValuePair();
		statePair.setKeyName("state");
		statePair.setKeyValue(state);
		parameterList.add(9, statePair);
		
		KeyAndValuePair zipPair = new KeyAndValuePair();
		zipPair.setKeyName("zip");
		zipPair.setKeyValue(zip);
		parameterList.add(10, zipPair);

		KeyAndValuePair towReasonPair = new KeyAndValuePair();
		towReasonPair.setKeyName("towReason");
		towReasonPair.setKeyValue(tow);
		parameterList.add(11, towReasonPair);

		KeyAndValuePair mileagePair = new KeyAndValuePair();
		mileagePair.setKeyName("mileage");
		mileagePair.setKeyValue(mileage);
		parameterList.add(12, mileagePair);

		KeyAndValuePair licensePlatePair = new KeyAndValuePair();
		licensePlatePair.setKeyName("licensePlate");
		licensePlatePair.setKeyValue(plate);
		parameterList.add(13, licensePlatePair);
	
		KeyAndValuePair bookmarkPair = new KeyAndValuePair();
		bookmarkPair.setKeyName("bookmark");
		String b = "false";
		if (bookmark) b = "true";
		bookmarkPair.setKeyValue(b);
		parameterList.add(14, bookmarkPair);
		
		KeyAndValuePair notetitlePair = new KeyAndValuePair();
		notetitlePair.setKeyName("notetitle");
		notetitlePair.setKeyValue(title);
		parameterList.add(15, notetitlePair);
		
		KeyAndValuePair notePair = new KeyAndValuePair();
		notePair.setKeyName("note");
		notePair.setKeyValue(note);
		parameterList.add(16, notePair);


		String resultno = WebServiceUtil.callWebservice(
				"createNewCaseBean", parameterList);
						
				return resultno;												


}
    public String editCase(){
	
			ArrayList parameterList = new ArrayList();
			
			KeyAndValuePair pictureCountPair = new KeyAndValuePair();
			pictureCountPair.setKeyName("howmanyPicture");
			pictureCountPair.setKeyValue("0");
			parameterList.add(0, pictureCountPair);

			KeyAndValuePair carMakePair = new KeyAndValuePair();
			carMakePair.setKeyName("carMake");
			carMakePair.setKeyValue(make);
			parameterList.add(1, carMakePair);

			KeyAndValuePair carModelPair = new KeyAndValuePair();
			carModelPair.setKeyName("carModel");
			carModelPair.setKeyValue(model);
			parameterList.add(2, carModelPair);

			KeyAndValuePair carColorPair = new KeyAndValuePair();
			carColorPair.setKeyName("carColor");
			carColorPair.setKeyValue(color);
			parameterList.add(3, carColorPair);

			KeyAndValuePair carTypePair = new KeyAndValuePair();
			carTypePair.setKeyName("carType");
			carTypePair.setKeyValue(type);
			parameterList.add(4, carTypePair);
			
			KeyAndValuePair carYearPair = new KeyAndValuePair();
			carYearPair.setKeyName("carYear");
			carYearPair.setKeyValue(year);
			parameterList.add(5, carYearPair);

			KeyAndValuePair vinPair = new KeyAndValuePair();
			vinPair.setKeyName("vin");
			vinPair.setKeyValue(vin);
			parameterList.add(6, vinPair);

			KeyAndValuePair addressPair = new KeyAndValuePair();
			addressPair.setKeyName("address");
			addressPair.setKeyValue(add);
			parameterList.add(7, addressPair);
			
			KeyAndValuePair cityPair = new KeyAndValuePair();
			cityPair.setKeyName("city");
			cityPair.setKeyValue(city);
			parameterList.add(8, cityPair);
			
			KeyAndValuePair statePair = new KeyAndValuePair();
			statePair.setKeyName("state");
			statePair.setKeyValue(state);
			parameterList.add(9, statePair);
			
			KeyAndValuePair zipPair = new KeyAndValuePair();
			zipPair.setKeyName("zip");
			zipPair.setKeyValue(zip);
			parameterList.add(10, zipPair);

			KeyAndValuePair towReasonPair = new KeyAndValuePair();
			towReasonPair.setKeyName("towReason");
			towReasonPair.setKeyValue(tow);
			parameterList.add(11, towReasonPair);

			KeyAndValuePair mileagePair = new KeyAndValuePair();
			mileagePair.setKeyName("mileage");
			mileagePair.setKeyValue(mileage);
			parameterList.add(12, mileagePair);

			KeyAndValuePair licensePlatePair = new KeyAndValuePair();
			licensePlatePair.setKeyName("licensePlate");
			licensePlatePair.setKeyValue(plate);
			parameterList.add(13, licensePlatePair);
		
			KeyAndValuePair bookmarkPair = new KeyAndValuePair();
			bookmarkPair.setKeyName("bookmark");
			String b = "false";
			if (bookmark) b = "true";
			bookmarkPair.setKeyValue(b);
			parameterList.add(14, bookmarkPair);
			
			KeyAndValuePair notetitlePair = new KeyAndValuePair();
			notetitlePair.setKeyName("notetitle");
			notetitlePair.setKeyValue(title);
			parameterList.add(15, notetitlePair);
			
			KeyAndValuePair notePair = new KeyAndValuePair();
			notePair.setKeyName("note");
			notePair.setKeyValue(note);
			parameterList.add(16, notePair);
			
			KeyAndValuePair caseNumberPair = new KeyAndValuePair();
			caseNumberPair.setKeyName("caseNumber");
			caseNumberPair.setKeyValue(no);
			parameterList.add(17, caseNumberPair);
	
			String resultno = WebServiceUtil.callWebservice(
					"editCaseBean", parameterList);
							
					return resultno;												

    }
    
public String createNote(){
    	

		
		ArrayList parameterList = new ArrayList();

		KeyAndValuePair cnPair = new KeyAndValuePair();
		cnPair.setKeyName("caseNumber");
		cnPair.setKeyValue(caseNo);
		parameterList.add(0, cnPair);
		
		KeyAndValuePair bookmarkPair = new KeyAndValuePair();
		bookmarkPair.setKeyName("bookmark");
		String b = "false";
		if (bookmark) b = "true";
		bookmarkPair.setKeyValue(b);
		parameterList.add(1, bookmarkPair);
		
		KeyAndValuePair notetitlePair = new KeyAndValuePair();
		notetitlePair.setKeyName("notetitle");
		notetitlePair.setKeyValue(title);
		parameterList.add(2, notetitlePair);
		
		KeyAndValuePair notePair = new KeyAndValuePair();
		notePair.setKeyName("note");
		notePair.setKeyValue(note);
		parameterList.add(3, notePair);


		String resultno = WebServiceUtil.callWebservice(
				"createNewNoteBean", parameterList);
			
				return resultno;

}
    





        
        private void createCaseProgress(){
//        	flagAll = true;

            progressDialog = new ProgressDialog(mC);
            progressDialog.setButton("cancel", new DialogInterface.OnClickListener() {
            	 public void onClick(DialogInterface dialog, int i) {
//            		 flagAll = false;
            	  dialog.cancel();
            	 }
            	});
            progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            	 public void onCancel(DialogInterface dialog) {

            	  progressDialog.dismiss();
            	  
            	 }
            	});
        	progressDialog.setCancelable(true);
        	progressDialog.setMessage("Processing, please wait!");

        	progressDialog.show();

        	new Thread(){  
      		  
                @Override  
                public void run() {  
                	caseNo = createCase();
                	if(!caseNo.equals("0"))
            		{
            		changePicNameToCase();
            		
            		
            		}
                	
                	
                    handler.sendEmptyMessage(1);  
                    
                }}.start();
        	
        	
        	
        	
        }
        private void editCaseProgress(){
//        	flagAll = true;
       
            progressDialog = new ProgressDialog(mC);
            progressDialog.setButton("cancel", new DialogInterface.OnClickListener() {
            	 public void onClick(DialogInterface dialog, int i) {
//            		 flagAll = false;
            	  dialog.cancel();
            	 }
            	});
            progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            	 public void onCancel(DialogInterface dialog) {

            	  progressDialog.dismiss();
            	  
            	 }
            	});
        	progressDialog.setCancelable(true);
        	progressDialog.setMessage("Processing, please wait!");
      
        	progressDialog.show();

        	new Thread(){  
      		  
                @Override  
                public void run() {  

                	caseNo = editCase();
                	if(!caseNo.equals("0"))
                		{createNote();
                		changePicNameToCase();
                		
                		}
                	
                	
                	
                    handler.sendEmptyMessage(2);  
                    
                }}.start();
        	
        	
        	
        	
        }



		@Override
		public void onTabChanged(String tabId) {
			// TODO Auto-generated method stub
			

		    	

		    	TabWidget tabWidget = tabHost.getTabWidget();
		    	for (int i =0; i < tabWidget.getChildCount(); i++) {
		    		tabWidget.getChildAt(i).getLayoutParams().height = 120;
		    		TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
		    		tabWidget.getChildAt(i).setBackgroundColor(Color.GRAY);
		    		tv.setTextColor(Color.WHITE);
		    		tv.setTextSize(18);
			    }
		    	
		    	
		    	tabWidget.getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#f36c29"));
			
		}
 
}
